//将数据处理成树形结构
export const handleTree = (data, pid) => {
  function tree(id) {
    let arr = [];
    data
      .filter((item) => {
        return item.parent_id === id;
      })
      .forEach((item) => {
        arr.push({
          ...item,
          children: tree(item.message_id),
        });
      });
    return arr;
  }
  return tree(pid);
};

//将树形结构扁平化
export function extractTree(tree) {
  let deeptree = JSON.parse(JSON.stringify(tree));
  let result = [];
  deeptree.forEach((node) => {
    result.push(node);
    if (node.children && node.children.length > 0) {
      result = result.concat(extractTree(node.children));
    }
    delete node.children;
  });
  return result;
}
//生成唯一值
export function generateUniqueId() {
  const timestamp = new Date().getTime();
  const randomNum = Math.random().toString(36).substr(2, 9);
  return `${timestamp}${randomNum}`;
}
